package com.yljk.homedoctor.view.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.yljk.homedoctor.R;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

public class TumourDialog extends Dialog {


    public TumourDialog(@NonNull Context context) {
        super(context);
    }

    public TumourDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected TumourDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder{
        private Context context;
        private String tumour_title;
        private String tumour_message;
        private String disAgreeText;
        private String agreeText;
        private ImageView icon;
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private View layout;

        private int status;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.tumour_message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param
         * @return
         */
        public Builder setMessage(int message) {
            this.tumour_message = (String) context.getText(message);
            return this;
        }

        /***
         * 设置dialog状态来改变icon显示样式
         * 0 无状态
         * 1 失败
         * 2 开启
         * @param status
         * @return
         */
        public Builder setIcon(int status){
           this.status = status;
            return this;
        }


        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.tumour_title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.tumour_title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         OnClickListener listener) {
            this.agreeText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.agreeText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.disAgreeText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.disAgreeText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public TumourDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final TumourDialog dialog = new TumourDialog(context, R.style.Dialog);
            layout = inflater.inflate(R.layout.dialog_tumour, null);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.tumour_title)).setText(tumour_title);

            if (status == 0){
                ((ImageView)layout.findViewById(R.id.tumour_icon)).setVisibility(View.GONE);
            }else if (status == 1){
                ((ImageView)layout.findViewById(R.id.tumour_icon)).setVisibility(View.VISIBLE);
                Glide.with(context).load(R.mipmap.dialog_tumour_open_icon).transform(new CenterCrop()).into(((ImageView)layout.findViewById(R.id.tumour_icon)));
            }else if (status == 2){
                ((ImageView)layout.findViewById(R.id.tumour_icon)).setVisibility(View.VISIBLE);
                Glide.with(context).load(R.mipmap.dialog_tumour_faile_icon).transform(new CenterCrop()).into(((ImageView)layout.findViewById(R.id.tumour_icon)));
            }


            // set the confirm button
            if (agreeText != null) {
                ((TextView) layout.findViewById(R.id.tumour_agree))
                        .setText(agreeText);
                if (positiveButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.tumour_agree))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            }
            else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.tumour_agree).setVisibility(
                        View.GONE);
            }
//            // set the cancel button
            if (disAgreeText != null) {
                ((TextView) layout.findViewById(R.id.tumour_disagree))
                        .setText(disAgreeText);
                if (negativeButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.tumour_disagree))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            }else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.tumour_disagree).setVisibility(
                        View.GONE);
            }
            // set the content message
            if (tumour_message != null) {
                RichText.from(tumour_message).bind(this)
                        .showBorder(false)
                        .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
                        .into((TextView) layout.findViewById(R.id.tumour_message));
//                ((TextView) layout.findViewById(R.id.tumour_message)).setText(tumour_message);
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
            }

            dialog.setContentView(layout);

            return dialog;
        }

    }





}
